//
//  KFDTabBarController.m
//  KFD
//
//  Created by JIRENTIANXIANG on 6/8/15.
//  Copyright (c) 2015 KFD. All rights reserved.
//

#import "KFDTabBarController.h"
#import "KFDMarketViewController.h"
#import "KFDLiveViewController.h"
#import <Onboard/OnboardingViewController.h>
#import <Onboard/OnboardingContentViewController.h>
#import "ui_config.h"
#import "AppDelegate.h"
#import "KFDChat.h"
#import "KFDChatViewController.h"

@interface KFDTabBarController ()

@property (strong,nonatomic) NSMutableArray* navigationControllers;

@property (strong,nonatomic) UITableViewController* meVC;
@property (strong,nonatomic) UITableViewController* discoveryVC;

@end

@implementation KFDTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupMeVC];
    [self setupDiscoveryVC];
    
    self.navigationControllers=[NSMutableArray array];
    for (int i=0; i<4; i++) {
        [self.navigationControllers addObject:[UINavigationController new]];
    }
    [self.navigationControllers[0] pushViewController:[KFDMarketViewController new] animated:NO];
    [self.navigationControllers[1] pushViewController:[KFDLiveViewController new] animated:NO];
    [self.navigationControllers[2] pushViewController:self.discoveryVC animated:NO];
    [self.navigationControllers[3] pushViewController:self.meVC animated:NO];
    
    [self setViewControllers:self.navigationControllers];
    NSArray* imageArray=@[@"menubtn01", @"menubtn02", @"menubtn03", @"menubtn04"];
    NSArray* imageArraySelected= @[@"menubtn01-curr", @"menubtn02-curr", @"menubtn03-curr", @"menubtn04-curr"];
    for (NSInteger i = 0; i < 4; i ++) {
        UITabBarItem *item = self.tabBar.items[i];
        [item setImage:[[UIImage imageNamed:imageArray[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        [item setSelectedImage:[[UIImage imageNamed:imageArraySelected[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    }
    UITabBarItem* item0=self.tabBar.items[0];
    UITabBarItem* item1=self.tabBar.items[1];
    UITabBarItem* item2=self.tabBar.items[2];
    UITabBarItem* item3=self.tabBar.items[3];
    [item0 setTitle:@"行情"];
    [item1 setTitle:@"直播"];
    [item2 setTitle:@"发现"];
    [item3 setTitle:@"我的"];
    self.view.tintColor=KFD_RED;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(apnParsedAcition) name:KFDChatNotification object:nil];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KFDChatNotification object:nil];
}

-(void)apnParsedAcition
{
    dispatch_async(dispatch_get_main_queue(), ^{
        AppDelegate* appDelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
        if (appDelegate.chatUrl) {
            KFDChat* chat=[KFDChat new];
            chat.url=appDelegate.chatUrl;
            KFDChatViewController* chatVC=[KFDChatViewController new];
            chatVC.chat=chat;
            UIViewController* currentVC=self.selectedViewController;
            if ([currentVC isKindOfClass:[UINavigationController class]]) {
                [(UINavigationController*) currentVC pushViewController:chatVC animated:YES];
            }else{
                [self.selectedViewController.navigationController pushViewController:chatVC animated:YES];
            }
            appDelegate.chatUrl=nil;
        }
    });
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSUserDefaults* userDefaults=[NSUserDefaults standardUserDefaults];
    id launched=[userDefaults objectForKey:@"launched"];
    if (!launched) {
        OnboardingContentViewController* pageOne=[OnboardingContentViewController contentWithTitle:nil body:nil image:[UIImage imageNamed:@"启动页01.jpg"] buttonText:nil action:nil];
        OnboardingContentViewController* pageTwo=[OnboardingContentViewController contentWithTitle:nil body:nil image:[UIImage imageNamed:@"启动页02.jpg"] buttonText:nil action:nil];
        OnboardingContentViewController* pageThree=[OnboardingContentViewController contentWithTitle:nil body:nil image:[UIImage imageNamed:@"启动页03.jpg"] buttonText:nil action:nil];
        OnboardingContentViewController* pageFour=[OnboardingContentViewController contentWithTitle:nil body:nil image:[UIImage imageNamed:@"启动页04.jpg"] buttonText:nil action:nil];
        OnboardingViewController* onboardVC=[OnboardingViewController onboardWithBackgroundImage:nil contents:@[pageOne,pageTwo,pageThree,pageFour]];
        [self presentViewController:onboardVC animated:YES completion:nil];
    }
    [userDefaults setObject:@"" forKey:@"launched"];
}

-(void)setupMeVC
{
    self.meVC=[[UIStoryboard storyboardWithName:@"Me" bundle:nil] instantiateViewControllerWithIdentifier:@"KFDMeViewController"];
}

-(void)setupDiscoveryVC
{
    self.discoveryVC=[[UIStoryboard storyboardWithName:@"Discovery" bundle:nil] instantiateViewControllerWithIdentifier:@"KFDDiscoveryViewController"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
